package com.wechat.wechat.service.impl;

import com.wechat.wechat.dto.CartDTO;
import com.wechat.wechat.entity.ProductInfo;
import com.wechat.wechat.enums.ProduceStatusEnum;
import com.wechat.wechat.enums.ResultEnum;
import com.wechat.wechat.exception.SellException;
import com.wechat.wechat.repository.ProductInfoRepository;
import com.wechat.wechat.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @author NN
 * @2020/2/20 10:13
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductInfoRepository repository;
    @Override
    public ProductInfo findOne(String productId) {
        return repository.findOne(productId);
    }

    @Override
    public List<ProductInfo> findAllUp() {
        return repository.findByProductStatus(ProduceStatusEnum.UP.getCode());
    }

    @Override
    public Page<ProductInfo> findAll(Pageable pageable){
        return repository.findAll(pageable);
    }

    @Override
    public ProductInfo save(ProductInfo productInfo) {
        return repository.save(productInfo);
    }

    @Override
    @Transactional
    public void decreaseStock(List<CartDTO> cartDTOList) {
        for(CartDTO cartDTO: cartDTOList)
        {
            //根据id查找商品
            ProductInfo productInfo=repository.findOne(cartDTO.getProductID());
            if(productInfo==null)
            {
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }
            //判断库存是否够
            Integer stock = productInfo.getProductStock()-cartDTO.getProductQuantity();
            if(stock < 0)
            {
                throw new SellException(ResultEnum.PRODUCT_STOCK_ERROR);
            }
            //更新库存
            productInfo.setProductStock(stock);
            repository.save(productInfo);
        }
    }

    @Override
    @Transactional
    public void increaseStock(List<CartDTO> cartDTOList) {
        for(CartDTO cartDTO: cartDTOList)
        {
            //根据id查找商品
            ProductInfo productInfo=repository.findOne(cartDTO.getProductID());
            if(productInfo==null)
            {
                throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
            }
            //更新库存
            Integer stock = productInfo.getProductStock()+cartDTO.getProductQuantity();
            productInfo.setProductStock(stock);
            repository.save(productInfo);
        }
    }

    @Override
    public ProductInfo onSale(String productId) {
        ProductInfo productInfo=repository.findOne(productId);
        if(productId==null)
        {
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        if(productInfo.getProductStatus() == ProduceStatusEnum.UP.getCode())
        {
            throw new SellException(ResultEnum.PRODUCT_STATUS_ERROR);
        }
        productInfo.setProductStatus(ProduceStatusEnum.UP.getCode());
        return repository.save(productInfo);
    }

    @Override
    public ProductInfo offSale(String productId) {
        ProductInfo productInfo=repository.findOne(productId);
        if(productId==null)
        {
            throw new SellException(ResultEnum.PRODUCT_NOT_EXIST);
        }
        if(productInfo.getProductStatus() == ProduceStatusEnum.DOWN.getCode())
        {
            throw new SellException(ResultEnum.PRODUCT_STATUS_ERROR);
        }
        productInfo.setProductStatus(ProduceStatusEnum.DOWN.getCode());
        return repository.save(productInfo);
    }
}
